import java.util.Stack;
//括号匹配
public class Solution2 {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < s.length(); i++) {
            Character ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                if(stack.empty()) {
                    return false;
                }
                Character topL = stack.peek();
                if(topL == '(' && ch == ')' || topL == '{' && ch == '}' ||
                        topL == '[' && ch == ']') {
                    stack.pop();
                }else return false;
            }
        }
        if(!stack.empty()) {
            return false;
        }
        return true;
    }
}
